package com.gobang.gobang;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ServerApplication extends Application {
    private GobangServer server;
    private Label statusLabel;
    private Button toggleButton;

    @Override
    public void start(Stage stage) {
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        
        statusLabel = new Label("服务器未启动");
        toggleButton = new Button("启动服务器");
        toggleButton.setOnAction(e -> toggleServer());
        
        root.getChildren().addAll(statusLabel, toggleButton);
        
        Scene scene = new Scene(root, 300, 100);
        stage.setTitle("五子棋服务器");
        stage.setScene(scene);
        stage.show();
        
        stage.setOnCloseRequest(e -> {
            if (server != null) {
                server.stop();
            }
            Platform.exit();
        });
    }
    
    private void toggleServer() {
        if (server == null || !server.isRunning()) {
            startServer();
        } else {
            stopServer();
        }
    }
    
    private void startServer() {
        server = new GobangServer();
        Thread serverThread = new Thread(() -> {
            server.start();
        });
        serverThread.setDaemon(true);
        serverThread.start();
        
        statusLabel.setText("服务器运行中...");
        toggleButton.setText("停止服务器");
    }
    
    private void stopServer() {
        if (server != null) {
            server.stop();
            server = null;
        }
        statusLabel.setText("服务器已停止");
        toggleButton.setText("启动服务器");
    }

    public static void main(String[] args) {
        launch();
    }
} 